package com.hulman.oms.service;

import com.hulman.oms.bean.WeatherInfo;
import com.hulman.oms.bean.WeatherResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Service
public class WeatherService
{
    @Value("${amap.api.key}")
    private String apiKey;
    @Value("${amap.city.code}")
    private String cityCode;
    @Autowired
    private RestTemplate restTemplate;

    public WeatherInfo getWeatherInfo()
    {
        String url = "https://restapi.amap.com/v3/weather/weatherInfo?key={key}&city={city}";
        WeatherResponse weatherResponse = restTemplate.getForObject(url, WeatherResponse.class, apiKey, cityCode);
        if (weatherResponse.getLives().size() > 0)
        {
            WeatherInfo weatherInfo = new WeatherInfo();
            weatherInfo.setTemperature(weatherResponse.getLives().get(0).getWeather());
            weatherInfo.setWeather(weatherResponse.getLives().get(0).getWeather());
            return weatherInfo;
        } else
        {
            return null;
        }
    }
}
